package cn.wangxl.loglog.web;

import cn.wangxl.loglog.constants.Application;
import cn.wangxl.loglog.constants.LogTypeEnum;
import cn.wangxl.loglog.dao.MongoLogRepository;
import cn.wangxl.loglog.domain.BasicLogDO;
import cn.wangxl.loglog.qo.ErrorLogQuery;
import cn.wangxl.loglog.qo.LogQuery;
import cn.wangxl.loglog.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 1.错误信息全文检索；
 * db.petty_loan_service.createIndex({message:"text"})
 *
 * db.petty_loan_service.find({$text:{$search:"内容"}})
 */
@Slf4j
@RestController
@RequestMapping("/log/error")
public class ErrorLogController {

    @Autowired
    LogService logService;

    @GetMapping("/count")
    public Long count(ErrorLogQuery errorLogQuery){

        long count = logService.count(errorLogQuery,
                Application.getByName(errorLogQuery.getApplication()),
                LogTypeEnum.getByType(errorLogQuery.getLogType()).getTopic());
        return count;
    }


    @GetMapping("/query")
    public List<BasicLogDO> query(ErrorLogQuery errorLogQuery){

        List<BasicLogDO> list = logService.query(errorLogQuery,
                Application.getByName(errorLogQuery.getApplication()),
                LogTypeEnum.getByType(errorLogQuery.getLogType()).getTopic());
        return list;
    }
}
